﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.TrainingPerson
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-26 9:10:27]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		private Config clsConfig = new Config();		// 配置类对象
		/// <summary>
		/// 用户控制类
		/// </summary>
		private User.Component clsUserComp;
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsData = clsDatabase;

			clsUserComp = new User.Component(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nTrainingID", clsDatabase.TRAININGID, clsDatabase.listStructure[clsDatabase.TRAININGID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonID", clsDatabase.PERSONID, clsDatabase.listStructure[clsDatabase.PERSONID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strJobNumber", clsDatabase.JOBNUMBER, clsDatabase.listStructure[clsDatabase.JOBNUMBER].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonName", clsDatabase.PERSONNAME, clsDatabase.listStructure[clsDatabase.PERSONNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nHaveSignUp", clsDatabase.HAVESIGNUP, clsDatabase.listStructure[clsDatabase.HAVESIGNUP].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strIDCard", clsDatabase.IDCARD, clsDatabase.listStructure[clsDatabase.IDCARD].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nTime", clsDatabase.TIME, clsDatabase.listStructure[clsDatabase.TIME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("double", "dTheoryScore", clsDatabase.THEORYSCORE, clsDatabase.listStructure[clsDatabase.THEORYSCORE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("double", "dOperationScore", clsDatabase.OPERATIONSCORE, clsDatabase.listStructure[clsDatabase.OPERATIONSCORE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserID", clsDatabase.USERID, clsDatabase.listStructure[clsDatabase.USERID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonDepartmentID", clsDatabase.PERSONDEPARTMENTID, clsDatabase.listStructure[clsDatabase.PERSONDEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonDepartmentName", clsDatabase.PERSONDEPARTMENTNAME, clsDatabase.listStructure[clsDatabase.PERSONDEPARTMENTNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nInputUserID", clsDatabase.INPUTUSERID, clsDatabase.listStructure[clsDatabase.INPUTUSERID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nInputDepartmentID", clsDatabase.INPUTDEPARTMENTID, clsDatabase.listStructure[clsDatabase.INPUTDEPARTMENTID].strName));

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strTrainingName", 0, clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.NAME].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strTrainingStartDate", 0, clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.STARTDATE].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strTrainingEndDate", 0, clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.ENDDATE].strName, false));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nTrainingID", clsDatabase.listStructure[clsDatabase.TRAININGID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strIDCard", clsDatabase.listStructure[clsDatabase.IDCARD].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nHaveSignUp", clsDatabase.listStructure[clsDatabase.HAVESIGNUP].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nUserID", clsDatabase.listStructure[clsDatabase.INPUTUSERID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strJobNumber", clsDatabase.listStructure[clsDatabase.JOBNUMBER].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strPersonName", clsDatabase.listStructure[clsDatabase.PERSONNAME].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nPersonUserID", clsDatabase.listStructure[clsDatabase.USERID].strName, "="));
			
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nTrainingState", clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.STATE].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nType", clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.TYPE].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strTrainingStartDate", clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.STARTDATE].strName, ">=", true));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strTrainingEndDate", clsDatabase.clsTrainingDB.listStructure[clsDatabase.clsTrainingDB.ENDDATE].strName, "<=", true));
			
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-26 9:10:27]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询

		#region 表操作
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsAdd = new Entity();
			User.Entity clsUser = new User.Entity();
			Person.Component clsPersonComp = new Person.Component();
			Person.Entity clsPerson = new Person.Entity();

			clsAdd = (Entity)clsEntity;
			clsAdd.InputUserID = nUserID;
			clsAdd.InputDepartmentID = nDepartment;

			clsPerson = clsPersonComp.Get(clsAdd.JobNumber, clsAdd.PersonName, clsAdd.IDCard);

			if (clsPerson != null)
			{
				clsAdd.PersonDepartmentName = clsPerson.depchs;
				clsAdd.PersonDepartmentID = clsPerson.depid;
				clsAdd.PersonID = clsPerson.usysid;

				clsUser = clsUserComp.QueryByPersonID(clsAdd.strPersonID);
				if (clsUser != null)
				{
					clsAdd.UserID = clsUser.ID;
				}
				else
				{
					if (clsUserComp.Add(clsAdd.PersonID, clsAdd.JobNumber, clsAdd.PersonName, clsAdd.IDCard, ref clsUser))
					{
						clsAdd.UserID = clsUser.ID;
					}
					else
					{
						return false;
					}
				}

				//clsAdd.HaveSignUp = clsConfig.nSignUp_No;
				clsAdd.HaveSignUp = clsConfig.nSignUp_Yes;

				if (Check(clsAdd.TrainingID, clsAdd.UserID))
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "该员工已经添加！");
					return false;
				}
				else
				{
					return base.Add(clsAdd);
				}
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(page, clsAdd.PersonName + "用户信息错！");
				return false;
			}

			// 需要在此处检查输入的参培人员信息是否正确
			//clsPerson = clsPersonComp.QueryByID(clsAdd.PersonID);
			//if (clsPerson != null)
			//{
			//        if (clsPerson.gh == clsAdd.JobNumber)
			//        {
			//                if (clsPerson.xm == clsAdd.PersonName)
			//                {
			//                        if (clsPerson.sfzh == clsAdd.IDCard)
			//                        {
							
			//                        }
			//                        else
			//                        {
			//                                TonyNet4V1.ShowMessageAlert.ShowMessage(page, clsAdd.IDCard + "身份证错！");
			//                                return false;
			//                        }
			//                }
			//                else
			//                {
			//                        TonyNet4V1.ShowMessageAlert.ShowMessage(page, clsAdd.PersonName + "姓名错！");
			//                        return false;
			//                }
			//        }
			//        else
			//        {
			//                TonyNet4V1.ShowMessageAlert.ShowMessage(page, clsAdd.JobNumber + "工号错！");
			//                return false;
			//        }
			//}
			//else
			//{
			//        TonyNet4V1.ShowMessageAlert.ShowMessage(page, clsAdd.PersonID + "唯一码错！");
			//        return false;
			//}

		}

		/// <summary>
		/// 删除一个培训班所有人员名单
		/// </summary>
		/// <returns></returns>
		public bool DeleteOneTrainingAllPerson(int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.nTrainingID = nTrainingID;

			list = Query(clsQuery);

			for (int i = 0; i < list.Count; i++)
			{
				Delete(list[i]);
			}

			return true;
		}
		/// <summary>
		/// 一个学习者在一个培训班报名
		/// </summary>
		/// <returns></returns>
		public bool TrainingApply(int nID)
		{
			Entity clsEntity = new Entity();
			User.Component clsUserComp = new User.Component(page);

			clsEntity = GetOne(nID);
			clsEntity.HaveSignUp = clsConfig.nSignUp_Yes;
			clsEntity.UserID = clsUserComp.QueryByIDCard(clsEntity.IDCard).ID;

			return Modify(clsEntity);
		}

		/// <summary>
		/// 根据用户工号、姓名、身份证号进行一个培训班的报名
		/// </summary>
		/// <param name="nTrainingID">培训班ID</param>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strPersonName">姓名</param>
		/// <param name="strIDCard">身份证</param>
		/// <returns>报名成功返回真，否则返回假</returns>
		/// <remarks>先在TrainingPerson表中检查工号、姓名、身份证是否在培训班参培人员中，然后在用户表中检查此人是否已经有
		/// 用户名，有的话获得该用户信息报名，没有的话注册新用户再报名</remarks>
		public bool TrainingApply(int nTrainingID, string strJobNumber, string strPersonName, string strIDCard)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			Entity clsTrainingPerson = new Entity();
			User.Component clsUserComp = new User.Component(page);
			User.Entity clsUser = new User.Entity();

			clsQuery.nTrainingID = nTrainingID;
			clsQuery.strJobNumber = strJobNumber;
			clsQuery.strPersonName = strPersonName;
			clsQuery.strIDCard = strIDCard;

			list = Query(clsQuery);

			if (list.Count > 0)
			{
				clsTrainingPerson = list[0];

				clsUser = clsUserComp.QueryByIDCard(clsTrainingPerson.IDCard);
				if (clsUser != null)
				{
					return TrainingApply(clsTrainingPerson.ID);
				}
				else
				{
					clsUser = new User.Entity();
					clsUser.Username = Guid.NewGuid().ToString();
					clsUser.Password = clsConfig.strInitPassword;
					clsUser.UserGroupID = clsConfig.nLearnerUserGroupID;
					clsUser.JobNumber = strJobNumber;
					clsUser.PersonName = strPersonName;
					clsUser.IDCard = strIDCard;
					clsUser.PersonID = clsTrainingPerson.PersonID;
					clsUser.Register = false;

					if (clsUserComp.Add(clsUser))
					{

						clsUser = clsUserComp.QueryByIDCard(strIDCard);
						if (clsUser != null)
						{
							return TrainingApply(clsTrainingPerson.ID);
						}
						else
						{
							TonyNet4V1.ShowMessageAlert.ShowError(page, "获得最新插入的用户表记录出错！");
							return false;
						}
					}
					else
					{
						TonyNet4V1.ShowMessageAlert.ShowError(page, "向Users表新添加记录出错！");
						return false;
					}
				}
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 给一个学员累加一个培训班在线学习时间
		/// </summary>
		/// <param name="nTrainingID">Training表ID</param>
		/// <param name="nTimeLength">页面刷新间隔</param>
		/// <returns></returns>
		public bool CumulativeTime(int nTrainingID, int nTimeLength)
		{
			Entity clsEntity = new Entity();
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.nTrainingID = nTrainingID;
			clsQuery.nPersonUserID = nUserID;

			list = Query(clsQuery);
			clsEntity = list[0];

			clsEntity.Time = clsEntity.Time + nTimeLength;

			return Modify(clsEntity);
		}
		#endregion 表操作

		/// <summary>
		/// 查询一个学习者能报名参加的培训班
		/// </summary>
		/// <param name="strIDCard">身份证</param>
		/// <returns></returns>
		public List<Entity> QueryOneLearnerCanApplyTraining(string strIDCard)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.strIDCard = strIDCard;
			clsQuery.nTrainingState = clsConfig.nTrainingState_Apply;
			clsQuery.nHaveSignUp = clsConfig.nSignUp_No;

			return Query(clsQuery);
		}
		/// <summary>
		/// 查询一个学习者报名参加的已经开课的培训班
		/// </summary>
		/// <param name="nUserID">学习者用户ID</param>
		/// <returns>由培训班ID组成的字符串，例如：1, 2, 3</returns>
		public string QueryOneLearnerTraining(int nUserID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			string strTrainingIDs = "";

			clsQuery.nPersonUserID = nUserID;
			clsQuery.nTrainingState = clsConfig.nTrainingState_Commencement;
			clsQuery.nType = clsConfig.nTrainingType_OnLine;
			list = Query(clsQuery);
			for (int i = 0; i < list.Count; i++)
			{
				strTrainingIDs = strTrainingIDs + list[i].TrainingID.ToString() + ", ";
			}

			strTrainingIDs = TonyNet4V1.CommonFunction.ExceptString(", ", strTrainingIDs);

			return strTrainingIDs;
		}
		

		/// <summary>
		/// 检查报名时添加的信息，是否在参加培训人员名单中
		/// </summary>
		/// <param name="strIDCard">身份证</param>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strPersonName">姓名</param>
		/// <param name="nTrainingID">培训班ID</param>
		/// <returns>查找到的的报名人实体，没有找到返回空</returns>
		public Entity CheckSignUpInfo(string strJobNumber, string strPersonName, string strIDCard, int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strJobNumber = strJobNumber;
			clsQuery.strPersonName = strPersonName;
			clsQuery.strIDCard = strIDCard;
			clsQuery.nTrainingID = nTrainingID;

			list = Query(clsQuery);
			if (list.Count > 0)
			{
				return list[0];
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 根据培训班ID查找该培训班参培人员
		/// </summary>
		/// <param name="nTrainingID">培训班ID</param>
		/// <returns>一个培训班所有参培人员</returns>
		public List<Entity> QueryByTrainingID(int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nTrainingID = nTrainingID;

			return Query(clsQuery);
		}

		/// <summary>
		/// 检查一个用户是否在一个培训班里
		/// </summary>
		/// <param name="nTrainingID">培训班ID</param>
		/// <param name="nUserID">用户ID</param>
		/// <returns></returns>
		public bool Check(int nTrainingID, int nUserID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nPersonUserID = nUserID;
			clsQuery.nTrainingID = nTrainingID;
			if (Query(clsQuery).Count > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
			
		}

		/// <summary>
		/// 根据学习用户的UserID查询
		/// </summary>
		/// <param name="nLearnerUserID"></param>
		/// <returns></returns>
		public List<Entity> QueryByLearnerUserID(int nLearnerUserID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nPersonUserID = nLearnerUserID;

			return Query(clsQuery);
		}
	}
}
